import Vue from 'vue'
import Login from "@/views/login/Login";
import VueRouter from 'vue-router';
import LoginDgut from "@/views/login/LoginDgut";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'root',
    redirect: "/login"
  },
  {
   path:'/login/dgut',
   component: LoginDgut
  },
  {
    path:"/login",
    name: 'login',
    component: Login
  },
  {
    path: '/user',
    name: 'user',
    component: ()=> import("@/views/user/User")
  },
  {
    path:'/admin',
    name: 'admin',
    component: ()=> import("@/views/admin/Admin"),
    children:[
      {path:'', component: ()=>import("@/views/admin/childComps/Index")},
      {path:'index',component:()=>import("@/views/admin/childComps/Index")},
      {
        path:'accountManagement',
        component: ()=>import("@/views/admin/childComps/AccountManagement")
      },
      {
        path:'infoManagement',
        component: ()=>import("@/views/admin/childComps/infoManagement/InfoManagement"),
        children:[
          {path:'', component:()=>import("@/views/admin/childComps/infoManagement/BasicInfo")},
          {path:'basicInfo', component: ()=>import("@/views/admin/childComps/infoManagement/BasicInfo")},
          {path:'dailyFormInfo', component:()=>import("@/views/admin/childComps/infoManagement/DailyFormInfo")},
          {path:'noFillList', component:()=>import("@/views/admin/childComps/infoManagement/NoFillList")}
        ]
      },
      {
        path: 'dataStatistics', component: ()=>import("@/views/admin/childComps/DataStatistics")
      }
    ]

  }
]

const router = new VueRouter({
  mode:'history',
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.path=="/login"||to.path=="/"||to.path=="/login/dgut"){
    next();
  }
  else{  //不是登录页面
    if(window.localStorage.getItem("springBoot-token")==null){  //取不到token
      next('/login');
    }
    if(to.path.match(/user/)){  //用户界面
      console.log(from.path);
      if(window.localStorage.getItem("role")&&window.localStorage.getItem("role")==="ROLE_user"){
        next();
      }
      else next(from.path);
    }
    if(to.path.match(/admin/)){
      console.log(from.path);
      if(window.localStorage.getItem("role")&&window.localStorage.getItem("role")==="ROLE_admin"){
        next();
      }
      else next(from.path);
    }
  }
})
export default router
